package System;

import java.util.logging.*;
import java.io.*;
import java.util.*;
import java.text.*;
/**
 * A logging facility. Composed of a logger and custom formatter.
 *
 */

/**
 * @author Nadav
 *
 */
public class SystemLogger
{
	private static SystemLogger instance = null;
    private static Logger logger;
    final private Format formatter = new SimpleDateFormat("HH:mm:ss");
    private static Handler fh;
        
    /**
     * empty ctor
     */
    protected SystemLogger() {
        // Exists only to defeat instantiation.
    }
    
    
    /**
     * an implementation of Singleton class, only one instance of the SystemLogger is created
     * and returned as a result.
     * 
     * @return instance on the logger class
     */
    public static SystemLogger getInstance() {
    	if (instance == null) {
    		instance = new SystemLogger();
    		logger = Logger.getLogger("systemLog");

            try {
            	boolean append = true;
            	fh = new FileHandler("SystemLog.log", append);

			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
            //use a custom formatter we created to comply with assignment
            fh.setFormatter(new SimpleFormatter());
            logger.addHandler(fh);
            logger.setLevel(Level.FINEST);
            
            return instance;
    	}
    	else
    		return instance;
    }
    

    /**
     * Sends a message to the logger.
     * The message 'message' will appear as:
     * 'message at HH:mm:ss'.
     * 
     * @param message A normal text message.
     */
    public synchronized void send(String message)
    {
        String s = formatter.format(new Date());
        logger.finest(message+" at "+s);
    }
    
    public synchronized void closeLogger()
    {
        logger.removeHandler(fh);
        fh.close();
    }
}

